package com.grad.webstudy.controller;

import com.grad.webstudy.annota.LoginAuthorization;
import com.grad.webstudy.service.MessageService;
import com.grad.webstudy.vo.MessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:
 * @Author: zjf
 * @Date: 2020/1/28 10:04.
 */


//@ServerEndpoint("/websocket/{name}")
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;



    @GetMapping("/history/{rId}")
    @LoginAuthorization
    public List<MessageVo> queryHistoryMessages(@PathVariable("rId") Integer receiveId){
        return messageService.queryHistoryMessages(receiveId);
    }





//    @MessageMapping(WebSocketContants.FORETOSERVERPATH)//@MessageMapping和@RequestMapping功能类似，用于设置URL映射地址，浏览器向服务器发起请求，需要通过该地址。
//    @SendTo(WebSocketContants.PRODUCERPATH)//如果服务器接受到了消息，就会对订阅了@SendTo括号中的地址传送消息。
//    public MessageVo say(MessageVo message) throws Exception {
//        List<String> users = new ArrayList<>();
//        users.add("12");//此处写死只是为了方便测试,此值需要对应页面中订阅个人消息的userId
//        send2Users(users, new MessageVo());
//
//        return new MessageVo();
//    }
//
//
//    /**
//     * 广播
//     * 发给所有在线用户
//     *
//     * @param msg
//     */
//    public void sendMsg(MessageVo msg) {
//        template.convertAndSend(WebSocketContants.PRODUCERPATH, msg);
//    }
//
//    /**
//     * 发送给指定用户
//     * @param users
//     * @param msg
//     */
//    public void send2Users(List<String> users, MessageVo msg) {
//        users.forEach(userName -> {
//            template.convertAndSendToUser(userName, WebSocketContants.P2PPUSHPATH, msg);
//        });
//    }



}
